package com.singularity.common.service.impl;


import com.singularity.common.domain.dto.permission.PermissionInfoDTO;
import com.singularity.common.domain.dto.role.RoleInfoDTO;
import com.singularity.common.domain.entity.Permission;
import com.singularity.common.mapper.PermissionMapper;
import com.singularity.common.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 天
 * @description: 权限接口实现
 */
@Service
public class PermissionServiceImpl implements PermissionService {

    private final PermissionMapper permissionMapper;

    @Autowired
    public PermissionServiceImpl(PermissionMapper permissionMapper) {
        this.permissionMapper = permissionMapper;
    }

    @Override
    public List<PermissionInfoDTO> getPermissionInfo(List<RoleInfoDTO> roles) {
        List<Long> roleIds = roles.stream().map(RoleInfoDTO::getId).collect(Collectors.toList());
        //查询所有权限
        List<PermissionInfoDTO> permissionAllInfo = permissionMapper.selectAll(1);

        //查询角色拥有权限
        List<PermissionInfoDTO> permissionInfoDTOList = permissionMapper.selectByRoleIds(roleIds);

        return this.buildPermissionsInfoTree(permissionInfoDTOList, permissionAllInfo);
    }

    @Override
    public List<PermissionInfoDTO> getAllPermissionList(Integer status) {
        //查询所有权限
        List<PermissionInfoDTO> permissionAllInfo = permissionMapper.selectAll(status);

        //获取顶层权限
        List<PermissionInfoDTO> permissionInfoDTOList = permissionAllInfo.stream()
                .filter(permissionInfoDTO -> permissionInfoDTO.getPId() == 0).collect(Collectors.toList());
        return this.buildPermissionsInfoTree(permissionInfoDTOList, permissionAllInfo);
    }

    @Override
    public List<PermissionInfoDTO> getPermissionList(Long roleId) {
        //查询所有权限
        List<PermissionInfoDTO> permissionAllInfo = permissionMapper.selectAll(1);

        //查询角色拥有权限
        List<PermissionInfoDTO> permissionInfoDTOList = permissionMapper.selectByRoleId(roleId);
        return this.buildPermissionsInfoTree(permissionInfoDTOList, permissionAllInfo);
    }

    @Override
    public Boolean insert(Permission permission) {
        return permissionMapper.insert(permission) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean update(Permission permission) {
        return permissionMapper.updateById(permission) > 0;
    }

    /**
     * @param permissionInfoDTOList 角色权限
     * @param permissionAllInfo     所有权限
     * @return
     */
    private List<PermissionInfoDTO> buildPermissionsInfoTree(List<PermissionInfoDTO> permissionInfoDTOList, List<PermissionInfoDTO> permissionAllInfo) {
        // 遍历角色拥有权限
        permissionInfoDTOList.forEach(permissionInfoDTO -> {
            List<PermissionInfoDTO> chilePermissionInfoDTOList = permissionInfoDTO.getPermissions();
            // 遍历所有权限
            permissionAllInfo.forEach(childPermissionInfoDTO -> {
                // 判断权限之前的关系
                if (childPermissionInfoDTO.getPId().equals(permissionInfoDTO.getId())) {
                    chilePermissionInfoDTOList.add(childPermissionInfoDTO);
                }
            });
            // 递归构建权限树
            buildPermissionsInfoTree(chilePermissionInfoDTOList, permissionAllInfo);
        });
        return permissionInfoDTOList;
    }

}
